#include "sample_test.h"

#include "storage/sample.h"
#include "storage/set.h"

#include "cppunit/TestAssert.h"

#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>

#define LAMBDA 1e-5



using namespace nymphaea;
using namespace nymphaea::sample;

CPPUNIT_TEST_SUITE_REGISTRATION(SampleTest);

SampleTest::SampleTest(): mpM1(NULL), mpM2(NULL)
{
	/* NULL */
}

void SampleTest::setUp()
{
	mpM1 = new Sample(2, 2);
	mpM2 = new Sample(2, 2);
	mpM1->set_element_by_idx(0, 1, 1.0F);
	mpM2->set_element_by_idx(0, 1, 1.0F);
}

void SampleTest::tearDown()
{
	delete mpM1;
	delete mpM2;
	mpM1 = NULL;
	mpM2 = NULL;
}

void SampleTest::testLoads()
{
	std::string data;
	std::string line;
	std::ifstream in("data/unittest/matrices/example_matrix.json");
	while(std::getline(in, line)){
		data += line;
	}
	in.close();

	Sample a(1, 1);
	int ret = a.load_from_bytes(data);
	CPPUNIT_ASSERT_MESSAGE("load_from_bytes() failed.", ret==0);
	CPPUNIT_ASSERT_MESSAGE("size is match.", (3==a.get_row_size() && 2==a.get_col_size()));
	CPPUNIT_ASSERT_DOUBLES_EQUAL(a.get_element_by_idx(0, 0), 1.0F, LAMBDA);
	CPPUNIT_ASSERT_DOUBLES_EQUAL(a.get_element_by_idx(0, 1), 2.0F, LAMBDA);
	CPPUNIT_ASSERT_DOUBLES_EQUAL(a.get_element_by_idx(1, 0), 3.0F, LAMBDA);
	CPPUNIT_ASSERT_DOUBLES_EQUAL(a.get_element_by_idx(1, 1), 4.0F, LAMBDA);
	CPPUNIT_ASSERT_DOUBLES_EQUAL(a.get_element_by_idx(2, 0), 5.0F, LAMBDA);
	CPPUNIT_ASSERT_DOUBLES_EQUAL(a.get_element_by_idx(2, 1), 6.0F, LAMBDA);
	
	//2. test of single element matrix;
	//3. test of failed case;
}


